package tech.cn.note.word.fun;

import java.math.BigDecimal;

import cn.hutool.core.util.StrUtil;
import com.deepoove.poi.render.compute.RenderDataCompute;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import static tech.cn.note.utils.BigDecimalUtil.isPositive;


/**
 * 乙方应支付甲方金额
 */
@Service
@Slf4j
public class BPayA implements RenderFunction {
    @Override
    public String doCalculate(String[] fields, RenderDataCompute renderDataCompute) {
        if (fields == null || fields.length == 0) {
            return "";
        }
        if (fields.length == 3) {
            String partB = (String) renderDataCompute.compute(fields[0]);
            String party = (String) renderDataCompute.compute(fields[1]);
            BigDecimal numValue = (BigDecimal) renderDataCompute.compute(fields[2]);
            if (numValue == null) {
                return "0";
            }
            if (StrUtil.equals(partB, party)) {
                //交易对手为乙方
                //我方为甲方
                return isPositive(numValue) ? numValue.toString() : "0";
            } else {
                return isPositive(numValue) ? "0" : numValue.negate().toString();
            }
        }
        return "";
    }
}
